package com.mioto.ld.communication;

import cn.hutool.core.util.ObjectUtil;
import com.mioto.ld.module.device.model.DeviceFunctionLog;
import com.mioto.ld.module.device.model.DeviceFunctionLogPO;
import com.mioto.ld.module.device.service.IDeviceFunctionLogService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @author mioto-qinxj
 * @date 2024/5/27
 * @desc 设备功能执行记录处理
 */
@Slf4j
@Component
public class DeviceFunctionLogProcess {

    @Resource
    private IDeviceFunctionLogService deviceFunctionLogService;

    /**
     * 保存日志记录
     * @param deviceSn 设备序列号
     * @param identity 功能标识 - 对应设备上报协议的mtype
     */
    public void saveFunctionLog(String deviceSn,Integer identity,Date createDate){
        DeviceFunctionLogPO deviceFunctionLogPO = deviceFunctionLogService.findIdAndDeviceIdBySnAndIdentity(deviceSn,identity.toString());
        if (ObjectUtil.isNotNull(deviceFunctionLogPO)){
            DeviceFunctionLog deviceFunctionLog = new DeviceFunctionLog();
            deviceFunctionLog.setDeviceId(deviceFunctionLogPO.getDeviceId());
            deviceFunctionLog.setCreateDate(createDate);
            deviceFunctionLog.setFunctionId(deviceFunctionLogPO.getFunctionId());
            deviceFunctionLogService.insert(deviceFunctionLog);
        }
    }
}
